const mongoose=require("mongoose")
//暴露第三层函数
module.exports={
    getLogin3,
    getCheck3,
    getWantCheck3,
    argreeCheck3,
    getWantChange3,
    getChange3,
    argreeChange3,
    create3,
}
//登录
async function create3(data){
    console.log(data);
    try{
        const Alldata=await mongoose.model("userModel").create(data)
        console.log(Alldata);
        return {
            code: 1,
            message: "生成成功",
            result:Alldata
        }
    }catch(error){
        console.log(error);
        return {
            code: 0,
            message: "生成失败"
        };
    }
}
//登录
async function getLogin3(data){
    console.log(data);
    try{
        const Alldata=await mongoose.model("userModel").find(data);
        console.log(Alldata);
        return {
            code: 1,
            message: "登录成功",
            result:Alldata
        }
    }catch(error){
        console.log(error);
        return {
            code: 0,
            message: "登录失败"
        };
    }
}
//用户认证
async function getCheck3({user_id,idCard,idName}){
    // console.log(user_id,idCard,idName);
    try{
        // 修改
        const Alldata=await mongoose.model("userModel").updateOne(
            {_id:user_id},
            {
                isToCheck:true,
                idCard,
                idName
            }
        );
        console.log(Alldata);
        return {
            code: 1,
            message: "认证成功",
            result:Alldata
        }
    }catch(error){
        console.log(error);
        return {
            code: 0,
            message: "认证失败"
        };
    }
}
//获取需要认证的用户
async function getWantCheck3({limitpage,pageNow}){
    // console.log(user_id,idCard,idName);
    try{
        const total=await mongoose.model("userModel").find({
            isToCheck:true
        })
        const Alldata=await mongoose.model("userModel").find({
            isToCheck:true
        }).limit(limitpage-0).skip((pageNow-1)*limitpage)
        console.log(Alldata);
        return {
            code: 1,
            message: "获取成功",
            result:Alldata,
            total:total.length
        }
    }catch(error){
        console.log(error);
        return {
            code: 0,
            message: "获取失败"
        };
    }
}
//同意用户认证
async function argreeCheck3({isSuccess,user_id}){
     console.log(user_id,isSuccess);
    try{
        if(isSuccess==false){
              // 修改
                const Alldata=await mongoose.model("userModel").updateOne(
                    {_id:user_id},
                    {
                        isToCheck:false,
                        isCheck:false
                    }
                );
                console.log(Alldata);
                return {
                    code: 1,
                    message: "认证失败",
                    result:Alldata
                }
        }else{
            // 修改
        const Alldata=await mongoose.model("userModel").updateOne(
            {_id:user_id},
            {
                isToCheck:false,
                isCheck:true
            }
        );
        console.log(Alldata);
        return {
            code: 1,
            message: "认证成功",
            result:Alldata
        }
        }
        
    }catch(error){
        console.log(error);
        return {
            code: 0,
            message: "认证后台失败"
        };
    }
}

//********
//用户审核
async function getChange3({user_id}){
    // console.log(user_id,idCard,idName);
    try{
        // 修改
        const Alldata=await mongoose.model("userModel").updateOne(
            {_id:user_id},
            {
                isToMerchant:true,
            }
        );
        console.log(Alldata);
        return {
            code: 1,
            message: "等待审核",
            result:Alldata
        }
    }catch(error){
        console.log(error);
        return {
            code: 0,
            message: "失败"
        };
    }
}
//获取需要审核的用户
async function getWantChange3({limitpage,pageNow}){
    // console.log(user_id,idCard,idName);
    try{
        const total=await mongoose.model("userModel").find({
            isToMerchant:true
        })
        const Alldata=await mongoose.model("userModel").find({
            isToMerchant:true
        }).limit(limitpage-0).skip((pageNow-1)*limitpage)
        console.log(Alldata);
        return {
            code: 1,
            message: "获取成功",
            result:Alldata,
            total:total.length
        }
    }catch(error){
        console.log(error);
        return {
            code: 0,
            message: "获取失败"
        };
    }
}
//同意用户审核
async function argreeChange3({isSuccess,user_id}){
     console.log(user_id,isSuccess);
    try{
        if(isSuccess==false){
              // 修改
                const Alldata=await mongoose.model("userModel").updateOne(
                    {_id:user_id},
                    {
                        isToMerchant:false,
                        isMerchant:false
                    }
                );
                console.log(Alldata);
                return {
                    code: 1,
                    message: "认证失败",
                    result:Alldata
                }
        }else{
            // 修改
        const Alldata=await mongoose.model("userModel").updateOne(
            {_id:user_id},
            {
                isToMerchant:false,
                isMerchant:true
            }
        );
        console.log(Alldata);
        return {
            code: 1,
            message: "审核成功",
            result:Alldata
        }
        }
        
    }catch(error){
        console.log(error);
        return {
            code: 0,
            message: "审核后台失败"
        };
    }
}